package module4;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class SimpleCalculator extends JFrame implements ActionListener
{
	private JPanel baseP = null;
	private JTextField add1Txt = null, add2Txt = null, sub1Txt = null, sub2Txt = null,
					   multi1Txt = null, multi2Txt = null, div1Txt = null, div2Txt = null,
					   mod1Txt = null, mod2Txt = null;
	private JLabel addLbl = null, subLbl = null, multiLbl = null, divLbl = null, modLbl = null;
	private JLabel resultAdd = null, resultSub = null, resultMulti = null, resultDiv = null,
						resultMod = null;
						
	public SimpleCalculator()
	{
		add1Txt = new JTextField("", 15);
		add1Txt.addActionListener(this);
		add1Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (add1Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		add2Txt = new JTextField("", 15);
		add2Txt.addActionListener(this);
		add2Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (add2Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		sub1Txt = new JTextField("", 15);
		sub1Txt.addActionListener(this);
		sub1Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (sub1Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		sub2Txt = new JTextField("", 15);
		sub2Txt.addActionListener(this);
		sub2Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (sub2Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		multi1Txt = new JTextField("", 15);
		multi1Txt.addActionListener(this);
		multi1Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (multi1Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		multi2Txt = new JTextField("", 15);
		multi2Txt.addActionListener(this);
		multi2Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (multi2Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		div1Txt = new JTextField("", 15);
		div1Txt.addActionListener(this);
		div1Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (div1Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		div2Txt = new JTextField("", 15);
		div2Txt.addActionListener(this);
		div2Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (div2Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		mod1Txt = new JTextField("", 15);
		mod1Txt.addActionListener(this);
		mod1Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (mod1Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		mod2Txt = new JTextField("", 15);
		mod2Txt.addActionListener(this);
		mod2Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					Boolean flag = false;
					char ch = e.getKeyChar();
					String str = new String (mod2Txt.getText());
					for(int i = 0; i < str.length(); i++)
					{
						if(str.charAt(i) == '.')
						{
							flag = true;
						}
					}
					if((ch < '0' || ch > '9')&&(ch != '.'))
					{
						e.consume();
					}
					if(ch == '.' && flag == true)
					{
						e.consume();
					}
				}
			}
			);
		addLbl = new JLabel(" + ");
		subLbl = new JLabel(" - ");
		multiLbl = new JLabel(" * ");
		divLbl = new JLabel(" / ");
		modLbl = new JLabel(" % ");
		resultAdd = new JLabel(" = ");
		resultSub = new JLabel(" = ");
		resultMulti = new JLabel(" = ");
		resultDiv = new JLabel(" = ");
		resultMod = new JLabel(" = ");
		
		baseP = new JPanel(new GridLayout(5,4));
		baseP.add(add1Txt);
		baseP.add(addLbl);
		baseP.add(add2Txt);
		baseP.add(resultAdd);
		baseP.add(sub1Txt);
		baseP.add(subLbl);
		baseP.add(sub2Txt);
		baseP.add(resultSub);
		baseP.add(multi1Txt);
		baseP.add(multiLbl);
		baseP.add(multi2Txt);
		baseP.add(resultMulti);
		baseP.add(div1Txt);
		baseP.add(divLbl);
		baseP.add(div2Txt);
		baseP.add(resultDiv);
		baseP.add(mod1Txt);
		baseP.add(modLbl);
		baseP.add(mod2Txt);
		baseP.add(resultMod);
		baseP.setBorder(BorderFactory.createEmptyBorder(20,30,30,20));
		
		this.setTitle("Simple Calculator");
		this.getContentPane().add(baseP);
		this.setSize(400, 180);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == add1Txt)
		{
			add2Txt.requestFocus();
		}
		if(source == sub1Txt)
		{
			sub2Txt.requestFocus();
		}
		if(source == multi1Txt)
		{
			multi2Txt.requestFocus();
		}
		if(source == div1Txt)
		{
			div2Txt.requestFocus();
		}
		if(source == mod1Txt)
		{
			mod2Txt.requestFocus();
		}
		if(source == add2Txt)
		{
			if((add1Txt.getText().equals(""))||(add2Txt.getText().equals("")))
			{
				resultAdd.setText("Empty");
			}
			else
			{
				String str1 = add1Txt.getText();
				String str2 = add2Txt.getText();
				
				Double d1 = Double.parseDouble(str1);
				Double d2 = Double.parseDouble(str2);
				Double d3 = d1+d2;
				
				resultAdd.setText(" = "+d3);
			}
			sub1Txt.requestFocus();
		}
		if(source == sub2Txt)
		{
			if((sub1Txt.getText().equals(""))||(sub2Txt.getText().equals("")))
			{
				resultSub.setText("Empty");
			}
			else
			{
				String str1 = sub1Txt.getText();
				String str2 = sub2Txt.getText();
				
				Double d1 = Double.parseDouble(str1);
				Double d2 = Double.parseDouble(str2);
				Double d3 = d1-d2;
				
				resultSub.setText(" = "+d3);
			}
			multi1Txt.requestFocus();
		}
		if(source == multi2Txt)
		{
			if((multi1Txt.getText().equals(""))||(multi2Txt.getText().equals("")))
			{
				resultMulti.setText("Empty");
			}
			else
			{
				String str1 = multi1Txt.getText();
				String str2 = multi2Txt.getText();
				
				Double d1 = Double.parseDouble(str1);
				Double d2 = Double.parseDouble(str2);
				Double d3 = d1*d2;
				
				resultMulti.setText(" = "+d3);
			}
			div1Txt.requestFocus();
		}
		if(source == div2Txt)
		{
			if((div1Txt.getText().equals(""))||(div2Txt.getText().equals("")))
			{
				resultDiv.setText("Empty");
			}
			else
			{
				String str1 = div1Txt.getText();
				String str2 = div2Txt.getText();
				
				Double d1 = Double.parseDouble(str1);
				Double d2 = Double.parseDouble(str2);
				Double d3 = d1/d2;
				
				resultDiv.setText(" = "+d3);
			}
			mod1Txt.requestFocus();
		}
		if(source == mod2Txt)
		{
			if((mod1Txt.getText().equals(""))||(mod2Txt.getText().equals("")))
			{
				resultMod.setText("Empty");
			}
			else
			{
				String str1 = mod1Txt.getText();
				String str2 = mod2Txt.getText();
				
				Double d1 = Double.parseDouble(str1);
				Double d2 = Double.parseDouble(str2);
				Double d3 = d1%d2;
				
				resultMod.setText(" = "+d3);
			}
			add1Txt.requestFocus();
		}
	}
	
	public static void main(String [] args)
	{
		new SimpleCalculator();
	} 									    	
}